<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="topbarwrap wrap">
      <!-- 左侧文字 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧导航 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" class="avatar" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" @click="changeIsShowLoginModal(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { WeiXinLoginAPI } from "@/request/api.js";
import { mapMutations, mapState, mapActions } from "vuex";
export default {
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo:"userInfo/initUserInfo"
    }),
    ...mapActions({
      asyncShowToast: "showToast/asyncShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  created() {
    this.$router.onReady(async () => {
      console.log("ready-code:", this.$route.query.code);
      let pathCode = this.$route.query.code;

      if (pathCode) {
        let res = await WeiXinLoginAPI({
          code: pathCode,
        });
        console.log("res:", res);
        if (res.code == 0) {
          this.asyncShowToast({
            isShow: true,
            title: "扫码登录成功",
            type: "success",
          });
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          this.changeIsLogined(true);
          this.$router.push(this.$route.path);
          this.asyncChangeUserInfo();
        } else if (res.code == 400) {
          this.asyncShowToast({
            isShow: true,
            title: "请重新扫码",
            type: "danger",
          });
          this.changeIsShowLoginModal(true);
        } else if (res.code == 407) {
          this.asyncShowToast({
            isShow: true,
            title: "请进行第一次手机号码登录",
            type: "danger",
          });
          this.changeIsShowLoginModal(true);
          localStorage.setItem("uuid", res.uuid);
          this.$router.push(this.$route.path);
        }
      } else {
        console.log("非扫码登录，普通页面切换");
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));
        if (token) {
          this.asyncChangeUserInfo();
        } else {
          this.initUserInfo()
        }
      }
    });
  },
  // watch: {
  //   "$route.path": {
  //     handler() {
  //       let token = localStorage.getItem("x-auth-token");
  //       this.changeIsLogined(Boolean(token));
  //     },
  //   },
  // },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;

    .right {
      li {
        margin-left: 20px;
        float: left;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
      .avatar {
        width: 23px;
        height: 22px;
        border-radius: 13px;
        vertical-align: middle;
      }
    }
  }
}
</style>